/*
 * NewDataPackageDialog.java
 *
 * Created on Oct 4, 2007, 9:04 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.admin.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.fpdev.util.gui.GUIFactory;

/**
 *
 * @author demory
 */
public class CreateDataPackageDialog extends JDialog implements ActionListener {

  private StartupDialog startup_;
  
  private JTextField name_, desc_, initX_, initY_, fileField_;
  private JButton createBtn_, cancelBtn_, selectFileBtn_;
  private JTabbedPane tabbedPane_;

  private File shapefile_;
  
  public CreateDataPackageDialog(StartupDialog startup) {
    super(startup);
    startup_ = startup;
    
    JPanel mainPanel, center, buttonRow;
    
    center = new JPanel(new GridLayout(2,1)); //GUIFactory.newColumnPanel();
    //center.setAlignmentY(Component.LEFT_ALIGNMENT);
    name_ = GUIFactory.newTextField("", 16, 120, LEFT_ALIGNMENT);
    desc_ = GUIFactory.newTextField("", 0, 240, LEFT_ALIGNMENT);
    initX_ = GUIFactory.newTextField("", 0, 80);
    initY_ = GUIFactory.newTextField("", 0, 80);
    
    JPanel mainPropsPanel = GUIFactory.newColumnPanel();
    mainPropsPanel.setBorder(new CompoundBorder(new TitledBorder("Main Properties"), new EmptyBorder(4,4,4,4)));
    mainPropsPanel.add(GUIFactory.newLabel("Name (no spaces or special chars):", LEFT_ALIGNMENT));
    mainPropsPanel.add(name_);
    mainPropsPanel.add(Box.createVerticalStrut(4));
    mainPropsPanel.add(GUIFactory.newLabel("Description:", LEFT_ALIGNMENT));
    mainPropsPanel.add(desc_);
    
    
    JPanel shpPanel = new JPanel(new BorderLayout()); //GUIFactory.newColumnPanel();
    shpPanel.setBorder(new EmptyBorder(4,4,4,4));
    JPanel fileRow = new JPanel(new BorderLayout());
    fileField_ = GUIFactory.newTextField("(None Selected)", 0, 200);
    fileField_.setEditable(false);
    fileField_.setBorder(new CompoundBorder(new EmptyBorder(0,0,0,4), fileField_.getBorder()));
    selectFileBtn_ = GUIFactory.newButton("Select..", 80, this);
    fileRow.add(fileField_, BorderLayout.CENTER);
    fileRow.add(selectFileBtn_, BorderLayout.EAST);
    shpPanel.add(fileRow, BorderLayout.NORTH);

    JPanel scratchPanel = GUIFactory.newColumnPanel();
    scratchPanel.setBorder(new EmptyBorder(4,4,4,4));
    //center.add(Box.createVerticalStrut(8));
    scratchPanel.add(GUIFactory.newLabel("Default start location (x,y):", LEFT_ALIGNMENT));
    JPanel initXYRow = new JPanel();
    initXYRow.setLayout(new BoxLayout(initXYRow, BoxLayout.X_AXIS));
    initXYRow.setAlignmentX(LEFT_ALIGNMENT);
    initXYRow.add(initX_);
    initXYRow.add(Box.createHorizontalStrut(5));
    initXYRow.add(initY_);
    scratchPanel.add(initXYRow);
    //center.add(Box.createVerticalStrut(8));


    tabbedPane_ = new JTabbedPane();
    tabbedPane_.add("Empty Map", scratchPanel);
    tabbedPane_.add("From Shapefile", shpPanel);

    JPanel dataPanel = GUIFactory.newColumnPanel();
    dataPanel.add(Box.createVerticalStrut(8));
    JLabel l = new JLabel("Geographic Data Source:");
    l.setAlignmentX(LEFT_ALIGNMENT);
    dataPanel.add(l);
    tabbedPane_.setAlignmentX(LEFT_ALIGNMENT);
    dataPanel.add(tabbedPane_);

    center.add(mainPropsPanel);
    center.add(dataPanel);



    buttonRow = new JPanel();
    buttonRow.setBorder(new EmptyBorder(8,0,0,0));
    createBtn_ = GUIFactory.newButton("Create", 80, this);
    cancelBtn_ = GUIFactory.newButton("Cancel", 80, this);
    buttonRow.setLayout(new BoxLayout(buttonRow, BoxLayout.X_AXIS));
    buttonRow.add(Box.createHorizontalGlue());
    buttonRow.add(createBtn_);
    buttonRow.add(Box.createHorizontalStrut(5));
    buttonRow.add(cancelBtn_);
    buttonRow.add(Box.createHorizontalGlue());
    
    mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(center, BorderLayout.CENTER);
    mainPanel.add(buttonRow, BorderLayout.SOUTH);
    mainPanel.setBorder(new EmptyBorder(5,5,5,5));
    
    getContentPane().add(mainPanel);
    pack();
    setVisible(true);
    setLocationRelativeTo(null);
  }
    
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == createBtn_) {

      if(tabbedPane_.getSelectedIndex() == 0)
        startup_.createDataPackage(name_.getText(), desc_.getText(), new Double(initX_.getText()).doubleValue(), new Double(initY_.getText()).doubleValue());
      if(tabbedPane_.getSelectedIndex() == 1)
        startup_.createDataPackage(name_.getText(), desc_.getText(), shapefile_);
      this.setVisible(false);
    }
    if(e.getSource() == cancelBtn_) this.setVisible(false);

    if(e.getSource() == selectFileBtn_) {
      JFileChooser fileChooser = new JFileChooser();
      
      FileNameExtensionFilter filter = new FileNameExtensionFilter("Shapefiles", "shp");
      fileChooser.setFileFilter(filter);

      int result = fileChooser.showOpenDialog(null);

      if (result == JFileChooser.APPROVE_OPTION) {
        shapefile_ = fileChooser.getSelectedFile();
        fileField_.setText(shapefile_.getName());
      }
    }
  }
}
